{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e55bf9ba-c3a7-43d6-bed9-345eaee29dd5",
   "metadata": {},
   "source": [
    "## Customizing the Configuration\n",
    "\n",
    "If `$COVALENT_CONFIG_DIR` is defined in the user environment then the Covalent global configuration file is stored at `$COVALENT_CONFIG_DIR/covalent.conf`. Otherwise, the configuration is in  `~/.config/covalent/covalent.conf`.\n",
    "\n",
    "You can view and update the global configuration from a notebook. Updates are applied to the global configuration file.\n",
    "\n",
    "### Prerequisite\n",
    "\n",
    "Use the shell to determine the file path the Covalent configuration file:\n",
    "\n",
    "```\n",
    "$ echo $COVALENT_CONFIG_DIR\n",
    "$ ls -l ~/.config/covalent/covalent.conf\n",
    "-rw-r--r--  1 test1  staff  1405 Jan  2 14:05 /home/user/.config/covalent/covalent.conf\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eaaaa227-e941-4516-b451-69bbbb408b06",
   "metadata": {},
   "source": [
    "### Procedure\n",
    "\n",
    "1. Retrieve the configuration as a Python `dict` by using the Covalent `get_config()` function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "55bc1913-5507-417d-a6af-7bb989256f4c",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'sdk': {'config_file': '/Users/mini-me/.config/covalent/covalent.conf',\n",
       "  'log_dir': '/Users/mini-me/.cache/covalent/test',\n",
       "  'log_level': 'error',\n",
       "  'enable_logging': 'true',\n",
       "  'executor_dir': '/Users/mini-me/.config/covalent/executor_plugins',\n",
       "  'no_cluster': 'false'},\n",
       " 'dispatcher': {'address': 'localhost',\n",
       "  'port': 48008,\n",
       "  'cache_dir': '/Users/mini-me/.cache/covalent',\n",
       "  'results_dir': 'results',\n",
       "  'log_dir': '/Users/mini-me/.cache/covalent',\n",
       "  'db_path': '/Users/mini-me/.local/share/covalent/dispatcher_db.sqlite'},\n",
       " 'dask': {'cache_dir': '/Users/mini-me/.cache/covalent',\n",
       "  'log_dir': '/Users/mini-me/.cache/covalent',\n",
       "  'mem_per_worker': 'auto',\n",
       "  'threads_per_worker': 1,\n",
       "  'num_workers': 8,\n",
       "  'scheduler_address': 'tcp://127.0.0.1:51918',\n",
       "  'dashboard_link': 'http://127.0.0.1:8787/status',\n",
       "  'process_info': \"<DaskCluster name='LocalDaskCluster' parent=12559 started>\",\n",
       "  'pid': 12561,\n",
       "  'admin_host': '127.0.0.1',\n",
       "  'admin_port': 51915},\n",
       " 'workflow_data': {'storage_type': 'local',\n",
       "  'base_dir': '/Users/mini-me/.local/share/covalent/workflow_data'},\n",
       " 'user_interface': {'address': 'localhost',\n",
       "  'port': 48008,\n",
       "  'dev_port': 49009,\n",
       "  'log_dir': '/Users/mini-me/.cache/covalent'},\n",
       " 'executors': {'local': {'log_stdout': 'stdout.log',\n",
       "   'log_stderr': 'stderr.log',\n",
       "   'cache_dir': '/Users/mini-me/.cache/covalent'},\n",
       "  'remote_executor': {'poll_freq': 15,\n",
       "   'remote_cache': '.cache/covalent',\n",
       "   'credentials_file': ''},\n",
       "  'dask': {'log_stdout': 'stdout.log',\n",
       "   'log_stderr': 'stderr.log',\n",
       "   'cache_dir': '/Users/mini-me/.cache/covalent'}}}"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import covalent as ct\n",
    "ct.get_config()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eace12ee-f1e9-43af-9a97-8fae2c11082d",
   "metadata": {},
   "source": [
    "2. Retrieve individual settings by specifying them in the `get_config()` function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8138b892-0483-4230-acf9-09ebffe6327c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'localhost'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ct.get_config(\"dispatcher.address\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "77f4b56e-4c5b-4b2f-9c80-6b14bbfbeb38",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'dispatcher.address': 'localhost', 'dispatcher.port': 48008}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ct.get_config([\"dispatcher.address\", \"dispatcher.port\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5c3506d-7a57-4d34-a21a-aae5f1049934",
   "metadata": {},
   "source": [
    "3. Set configuration parameters using the Covalent `set_config()` function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "256a7236-ae2a-4e86-8814-8b6c3b581b44",
   "metadata": {},
   "outputs": [],
   "source": [
    "ct.set_config(\"sdk.enable_logging\", \"false\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32da81c7",
   "metadata": {},
   "source": [
    "4. You can set multiple configuration parameters by passing them in a dictionary to the `set_config` function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2239aaf7-dda5-4042-9e6b-74a3b799c68a",
   "metadata": {},
   "outputs": [],
   "source": [
    "ct.set_config({\n",
    "    \"sdk.enable_logging\": \"true\",\n",
    "    \"sdk.log_level\": \"debug\"\n",
    "})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d162bd5d-847b-43e9-a9ee-aa958e6fc8fc",
   "metadata": {},
   "source": [
    "5. Of course, you can edit the config file directly. If you edit the config file, reload it into a Python environment using the Covalent `reload_config()` function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "541cd488-ec6a-4970-a37c-dd3c943ff730",
   "metadata": {},
   "outputs": [],
   "source": [
    "ct.reload_config()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00ed359f-d68d-44ee-9b3a-1009bc8e89f8",
   "metadata": {},
   "source": [
    "```bash\n",
    "$ covalent restart\n",
    "Covalent server has stopped.\n",
    "Covalent server has started at http://localhost:48008\n",
    "\n",
    "$  cat ~/.config/covalent/covalent.conf\n",
    "[sdk]\n",
    "config_file = \"/home/user/.config/covalent/covalent.conf\"\n",
    "log_dir = \"/home/user/.cache/covalent/test\"\n",
    "log_level = \"debug\"\n",
    "enable_logging = \"true\"\n",
    "executor_dir = \"/home/user/.config/covalent/executor_plugins\"\n",
    "no_cluster = \"false\"\n",
    "\n",
    "[dispatcher]\n",
    "address = \"localhost\"\n",
    "port = 48008\n",
    "cache_dir = \"/home/user/.cache/covalent\"\n",
    "results_dir = \"results\"\n",
    "log_dir = \"/home/user/.cache/covalent\"\n",
    "db_path = \"/home/user/.local/share/covalent/dispatcher_db.sqlite\"\n",
    "\n",
    "[dask]\n",
    "cache_dir = \"/home/user/.cache/covalent\"\n",
    "log_dir = \"/home/user/.cache/covalent\"\n",
    "mem_per_worker = \"auto\"\n",
    "threads_per_worker = 1\n",
    "num_workers = 8\n",
    "scheduler_address = \"tcp://127.0.0.1:56006\"\n",
    "dashboard_link = \"http://127.0.0.1:8787/status\"\n",
    "process_info = \"<DaskCluster name='LocalDaskCluster' parent=4210 started>\"\n",
    "pid = 4214\n",
    "admin_host = \"127.0.0.1\"\n",
    "admin_port = 55996\n",
    "\n",
    "[workflow_data]\n",
    "storage_type = \"local\"\n",
    "base_dir = \"/home/user/.local/share/covalent/workflow_data\"\n",
    "\n",
    "[user_interface]\n",
    "address = \"localhost\"\n",
    "port = 48008\n",
    "dev_port = 49009\n",
    "log_dir = \"/home/user/.cache/covalent\"\n",
    "\n",
    "[executors.local]\n",
    "log_stdout = \"stdout.log\"\n",
    "log_stderr = \"stderr.log\"\n",
    "cache_dir = \"/home/user/.cache/covalent\"\n",
    "\n",
    "[executors.remote_executor]\n",
    "poll_freq = 15\n",
    "remote_cache = \".cache/covalent\"\n",
    "credentials_file = \"\"\n",
    "\n",
    "[executors.dask]\n",
    "log_stdout = \"stdout.log\"\n",
    "log_stderr = \"stderr.log\"\n",
    "cache_dir = \"/home/user/.cache/covalent\"\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
